<template>
	<view class="content">
		<view class="app-title ac">
			<view class="fl" @click="back"><image class="back" src="/static/img/back.png"></image></view>首次入库
		</view>

		<view class="top-bg"></view>

		<view class="detail">
			<u-cell-group>
				<u-cell-item title="入库机构:" :value="org_name == '' ? '请选择' : org_name" @click="show_picker=true;columns=org_options"></u-cell-item>
				<u-cell-item title="入库车站:" :value="data_list.stationCode == '' ? '请选择' : data_list.stationCode" @click="show_picker = true;columns=station_options"></u-cell-item>
				<u-cell-item title="入 库 人:" :value="data_list.personId" :arrow="false"></u-cell-item>
				<!-- <u-cell-item title="器材类型:" :value="eqt_name == '' ? '请选择' : eqt_name" @click="show_picker=true;columns=type_options"></u-cell-item> -->
				<u-cell-item title="器材类型:" :value="eqt_name == '' ? '请选择' : eqt_name" @click="showTree"></u-cell-item>
				<u-field v-model="data_list.eqtModelNum" label="器材型号:" placeholder="请填写"></u-field>
				<u-field v-model="data_list.count" label="入库数量:" placeholder="请填写"></u-field>
				<u-field v-model="data_list.manufacturer" label="生产厂家:" placeholder="请填写"></u-field>
				<u-field v-model="data_list.memo" label="入库备注:" placeholder="请填写"></u-field>
			</u-cell-group>

			<view class="btn">
				<button class="default-btn" @click="pushData">入库</button>
			</view>
		</view>

		<view class="full" v-show="show_picker"></view>
		<u-select v-model="show_picker" :list="columns" label-name="text" @confirm="onConfirm" @cancel="onCancel">
		</u-select>
		<tki-tree ref="tkitree" :range="trees" rangeKey="text" @confirm="treeConfirm" @cancel="treeCancel" :selectParent="false" :multiple="false" :foldAll="false">
		</tki-tree>
	</view>
</template>

<script>
	import tkiTree from "@/components/tki-tree/tki-tree.vue"

	export default {
		components: {
			tkiTree
		},
		data() {
			return {
				trees: [],
				equip_options_tree: [],
				org_options: '',
				station_options: '',
				type_options: '',
				columns: [],
				data_list: {
					"count": 0, //入库数量
					"departId": "", // //入库机构
					"eqtList": [{
						"actualSdNote": "string", //实际上道位置备注
						"eqtModelNum": "string", //器材型号
						"eqtTypeId": "", //器材类型id
						"eqtTypeName": "string", //器材类型名称
						"id": "string", //器材ID
						"inStorageTime": "", //入库时间
						"inUseEqtVo": { //上道状态关联的设备信息
							"eqtModelNum": "string",
							"equTypeId": "string",
							"equTypeName": "string",
							"id": "1", //设备ID
							"note": "string",
							"orgId": "string", //所在机构
							"orgName": "string", //所在机构名字
							"sdId": "1", //上道设备位置ID
							"sdLocation": "string", //上道设备位置
							"sdTime": "", //器材上道时间
							"stationCode": "", //所在站
							"stationName": "string", // 所在站名字
							"zcCode": "string", //条码
							"zcList": [ //上道的器材信息
								{
									"code": "string", //条形码
									"departName": "string", //所在机构名称
									"eqtModelNum": "string", // 器材型号
									"equName": "string", //器材类型名称
									"id": "1", //器材ID
									"manufacturer": "string", //生产厂家
									"note": "string", //备注
									"ruKuTime": "", //入库时间
									"sdTime": "", //器材上道时间
									"stationName": "string", //所在站名称
									"storeAddress": "string" //存放位置
								}
							]
						},
						"manufacturer": "string", //生产厂家
						"note": "string", //备注
						"orgId": "string", //所在机构
						"orgName": "string", //所在机构名称
						"state": 0, //当前状态
						"stateName": "string", //状态中文
						"stationCode": "string", //所在站
						"stationName": "string", //所在车站名称
						"storeAddress": "string", //实际存放位置
						"zcCode": "string" //条码
					}],
					"eqtModelNum": "", //器材型号
					"eqtTypeId": "", //器材类型ID
					"eqtTypeName": "string", //器材类型名称
					"manufacturer": "", //生产厂家
					"memo": "", //备注
					"personId": uni.getStorageSync('username') || '', //入库人
					"stationCode": "" //入库车站
				},
				org_name: '',
				eqt_name: '',
				show_picker: false
			}
		},
		created() {
			this.initOrg();
		},
		methods: {
			back() {
				uni.navigateBack();
			},
			onConfirm(value, index) {
				if (value[0].extra.type == 'org') {
					this.data_list.departId = value[0].extra.id;
					this.org_name = value[0].label;
					this.getStation(value[0].extra.id);
				} else if (value[0].extra == 'station') {
					this.data_list.stationCode = value[0].label;
				} else if (value[0].extra.type == 'type') {
					this.data_list.eqtTypeId = value[0].extra.id;
					this.eqt_name = value[0].label;
				}
				this.onCancel();
			},
			onCancel() {
				this.show_picker = false;
			},
			treeConfirm(item) {
				this.data_list.eqtTypeId = item[0].id;
				this.eqt_name = item[0].text;
			},
			treeCancel(item) {},
			showTree() {
				this.trees = this.equip_options_tree;
				this.$refs.tkitree._show();
			},
			getStation(id) {
				this.fetch('GET', this.baseUrl() + '/home/getStations', {
					orgid: id
				}).then((res) => {
					if (res.returnCode == 1) {
						this.station_options = res.data;
						this.station_options.map((item) => {
							item.text = item.NAME;
							item.extra = 'station'
						})
					}
				})
			},
			initOrg() {
				this.fetch('GET', this.baseUrl() + '/home/getOrgTreeByPerson', {
					personId: this.getUserId()
				}).then((res) => {
					if (res.returnCode == 1) {
						this.org_options = res.data;
						this.org_options.map((item) => {
							item.children = '';
							item.extra = {
								type: 'org',
								id: item.id
							};
						})
					}
				});
				this.fetch('GET', this.baseUrl() + '/home/queryEqtTypeTree2', {}).then((res) => {
					if (res.returnCode == 1) {
						this.type_options = res.data;
						this.type_options.map((item) => {
							item.extra = {
								type: 'type',
								id: item.id
							};
						})
					}
				});
				this.fetch('GET', this.baseUrl() + '/home/queryEqtTypeTree3', {}).then((res) => {
					if (res.returnCode == 1) {
						this.equip_options_tree = Array.isArray(res.data) ? res.data : [res.data] || [];
					}
				});
			},
			pushData() {
				if (this.data_list.departId == '') return uni.showToast({title: '请选择机构',icon: 'none'});
				if (this.data_list.stationCode == '') return uni.showToast({title: '请选择车站',icon: 'none'});
				if (this.data_list.eqtTypeId == '') return uni.showToast({title: '请选择器材类型',icon: 'none'});
				if (!(/(^[1-9]\d*$)/.test(this.data_list.count))) return uni.showToast({title: '入库数量必须是正整数',icon: 'none'});
				this.fetch('POST', this.baseUrl() + '/eqtTZcController/firstInStorage', this.data_list, true).then((
					res) => {
					if (res.returnCode == 1) {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					}
				})
			}
		}
	}
</script>

<style scoped>
	.detail>.btn {
		position: absolute;
		bottom: 3%;
		left: 0;
		width: 100%;
		padding: 0 20px;
	}

	.default-btn {
		width: 98%;
		margin: 0 auto;
	}
</style>
